#include "collisionPlaneActor.h"
#include "notransformactor.h"
#include "datatypedefine.h"
#include "datamanager.h"
#include <osg\BlendFunc>
#include "scene.h"

const float collision_plane_height = 4.0f;
const QString collision_stateset_tag = "CollisionStateSet";

namespace VrDataCore
{
	CollisionPlaneDrawable::CollisionPlaneDrawable(CoreBase::ActorBase* owneractor,const QString& name)
		:Transformable(owneractor,name)
	{
		_root = new osg::Group;
		_planeShapeOne = new CoreBase::PlaneShape;
		//_planeShapeTwo = new CoreBase::PlaneShape;
		bool havestateset = DataManager::GetInstance().containsPublicStateSetByDrawableName(collision_stateset_tag);
		_ss = DataManager::GetInstance().getPublicStateSetByDrawableName(collision_stateset_tag);
		if(!havestateset)
		{
			_ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED) ;
			osg::ref_ptr<osg::Program> program = new osg::Program;
			program->setName("collision program");
			_ss->setAttributeAndModes( program.get(), osg::StateAttribute::ON );
			osg::BlendFunc* bf = new
				osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
			_ss->setAttributeAndModes(bf);
			_ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
			//_ss->setRenderBinDetails(20, "DepthSortedBin");
		}
	}

	void CollisionPlaneDrawable::setControlVertex(const osg::Vec3& start,const osg::Vec3& end)
	{
		_root->removeChildren(0,_root->getNumChildren());
		osg::Vec3 p3(end.x(),end.y(),end.z() + collision_plane_height);
		osg::Vec3 p4(start.x(),start.y(),start.z() + collision_plane_height);
		_planeShapeOne->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.5f));
		_planeShapeOne->reset(start,end,p3,p4,osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),
			osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
		//_planeShapeTwo->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.5f));
		//osg::Vec3 p5(start);
		//osg::Vec3 p6(end);
		//osg::Vec3 p7(p3);
		//osg::Vec3 p8(p4);
		//_planeShapeTwo->reset(p5,p8,p7,p6,osg::Vec2(0.0f,0.0f),osg::Vec2(1.0f,0.0f),
		//	osg::Vec2(1.0f,1.0f),osg::Vec2(0.0f,1.0f),_ss);
		_root->addChild(_planeShapeOne->getOSGNode());
		//_root->addChild(_planeShapeTwo->getOSGNode());
		getOSGNode()->asGroup()->addChild(_root);
	}

	void CollisionPlaneDrawable::setActive(bool active)
	{
		if(active)
		{
			_planeShapeOne->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.5f));
			//_planeShapeTwo->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.5f));
		}
		else
		{
			_planeShapeOne->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.0f));
			//_planeShapeTwo->setColor(osg::Vec4(0.0f,1.0f,1.0f,0.0f));
		}
	}

	CollisionPlaneDrawable::~CollisionPlaneDrawable()
	{}

	void CollisionPlaneDrawable::addedToScene(CoreBase::Scene* scene)
	{
		//if(scene == NULL)
		//	getSceneParent()->unRegisterCollidable(this);
		Transformable::addedToScene(scene);
		if(_haveInScene)
		{
			setActive(false);
			//getSceneParent()->registerCollidable(this);
			//setCollisionMesh(_root);
		}
	}

	CollisionPlaneActor::CollisionPlaneActor()
	{
		setClassName(CLASS_COLLISION_PLANE);
	}

	CollisionPlaneActor::~CollisionPlaneActor()
	{}

	void CollisionPlaneActor::createDrawable()
	{
		_drawable = new CollisionPlaneDrawable(this);
	}

	void CollisionPlaneActor::setControlVertex(const osg::Vec3& start,const osg::Vec3& end)
	{
		if(start!=end)
		{
			_pointStart = start;
			_pointEnd = end;
			dynamic_cast<CollisionPlaneDrawable*>(_drawable.get())->setControlVertex(_pointStart,_pointEnd);
		}
	}

	void CollisionPlaneActor::getControlVertex(osg::Vec3& start,osg::Vec3& end)
	{
		start = _pointStart;
		end = _pointEnd;
	}

	void CollisionPlaneActor::setActive(bool active)
	{
		_active = active;
		dynamic_cast<CollisionPlaneDrawable*>(_drawable.get())->setActive(_active);
	}

	bool CollisionPlaneActor::getActive()
	{
		return _active;
	}
}